package petrochina.company.electric.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.base.FragmentPagerAdapter;
import com.tencent.mmkv.MMKV;

import petrochina.company.electric.R;
import petrochina.company.electric.app.AppActivity;
import petrochina.company.electric.app.AppFragment;
import petrochina.company.electric.http.api.TaskListApi;
import petrochina.company.electric.ui.adapter.TabAdapter;
import petrochina.company.electric.ui.fragment.PowerFragment;
import petrochina.company.electric.widget.XCollapsingToolbarLayout;

/**
 * desc: 任务详情
 */
public final class TaskDetailActivity extends AppActivity implements TabAdapter.OnTabListener, ViewPager.OnPageChangeListener,
        XCollapsingToolbarLayout.OnScrimsListener {

    private static final String INTENT_KEY_IN_TASK = "task";

    public static void start(Context context, TaskListApi.Bean taskResult) {

        Intent intent = new Intent(context, TaskDetailActivity.class);
        intent.putExtra(INTENT_KEY_IN_TASK, taskResult);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private XCollapsingToolbarLayout mCollapsingToolbarLayout;
    private Toolbar mToolbar;

    private Gson gson;
    private MMKV mmkv;
    private TextView mAddressView;
    private TextView mHintView;
    private AppCompatImageView mSearchView;

    private RecyclerView mTabView;
    private ViewPager mViewPager;

    private TabAdapter mTabAdapter;
    private TaskListApi.Bean taskResult;
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.task_detail_activity;
    }

    @Override
    protected void initView() {
        gson = new Gson();
        mmkv = MMKV.defaultMMKV();
        taskResult = getParcelable(INTENT_KEY_IN_TASK);
        mCollapsingToolbarLayout = findViewById(R.id.ctl_home_bar);
        mToolbar = findViewById(R.id.tb_home_title);

        mAddressView = findViewById(R.id.tv_home_address);
        mHintView = findViewById(R.id.tv_home_hint);
        mSearchView = findViewById(R.id.iv_home_search);

        mTabView = findViewById(R.id.rv_task_tab);
        mViewPager = findViewById(R.id.vp_task_pager);

        mPagerAdapter = new FragmentPagerAdapter<>(this);
        String resultStr = gson.toJson(taskResult);
        mPagerAdapter.addFragment(PowerFragment.newInstance(0, resultStr), "停电");
        mPagerAdapter.addFragment(PowerFragment.newInstance(1, resultStr), "复电");
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(this);

        mTabAdapter = new TabAdapter(this);
        mTabView.setAdapter(mTabAdapter);

        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐
        ImmersionBar.setTitleBar(this, mToolbar);

        //设置渐变监听
        mCollapsingToolbarLayout.setOnScrimsListener(this);
    }

    @Override
    protected void initData() {
        mToolbar.setTitle(taskResult.getName());
        mHintView.setText(taskResult.getName());
        mTabAdapter.addItem("停电");
        mTabAdapter.addItem("复电");
        mTabAdapter.setOnTabListener(this);
    }

    /**
     * {@link TabAdapter.OnTabListener}
     */

    @Override
    public boolean onTabSelected(RecyclerView recyclerView, int position) {
        mViewPager.setCurrentItem(position);
        refreshData(position);
        return true;
    }

    /**
     * 切换时刷新
     *
     * @param position
     */
    private void refreshData(int position) {
        AppFragment<?> showFragment = mPagerAdapter.getShowFragment();
        if (showFragment instanceof PowerFragment) {
            PowerFragment powerFragment = (PowerFragment) showFragment;
            powerFragment.refreshData(position);
        }
    }

    /**
     * {@link ViewPager.OnPageChangeListener}
     */

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        if (mTabAdapter == null) {
            return;
        }
        mTabAdapter.setSelectedPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * CollapsingToolbarLayout 渐变回调
     * <p>
     * {@link XCollapsingToolbarLayout.OnScrimsListener}
     */
    @SuppressLint("RestrictedApi")
    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        getStatusBarConfig().statusBarDarkFont(shown).init();
        mAddressView.setTextColor(ContextCompat.getColor(this, shown ? R.color.black : R.color.white));
        mHintView.setBackgroundResource(shown ? R.drawable.home_search_bar_gray_bg : R.drawable.home_search_bar_transparent_bg);
        mHintView.setTextColor(ContextCompat.getColor(this, shown ? R.color.black60 : R.color.white60));
        mSearchView.setSupportImageTintList(ColorStateList.valueOf(getColor(shown ? R.color.common_icon_color : R.color.white)));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mViewPager.setAdapter(null);
        mViewPager.removeOnPageChangeListener(this);
        mTabAdapter.setOnTabListener(null);
    }

}